//
//  HJCPopoverAnimator.swift
//  新浪微博Swift
//
//  Created by 郝俊聪 on 15/5/14.
//  Copyright (c) 2015年 heima. All rights reserved.
//

import UIKit

class HJCPopoverAnimator: NSObject , UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning{
    
    //定义一个变量，记录是否演示转场
    var isPrensention = false
    
    //需要演示的窗口大小
    var presentedFrame = CGRectZero
    
    //代理方法，HJCPopoverPresentationControlle接管
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController!, sourceViewController source: UIViewController) -> UIPresentationController?{
        
        let popoverVC  = HJCPopoverPresentationController(presentedViewController: presented, presentingViewController: presenting)
        
        //给演示控制器赋值
        popoverVC.presentedFrame = presentedFrame
        
        return popoverVC
    }
    
    //自己提供显示演示控制器的动画
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        //标记为开始转场
        isPrensention = true
        
        return self
    }
    
    //自己提供移除演示控制器的动画
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        //标记为结束转场
        isPrensention = false
        
        return self
        
    }
    

    //转场时间
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval{
        
        return 0.5
        
    }
    
    
    //主动提供转场上下文
    func animateTransition(transitionContext: UIViewControllerContextTransitioning){
        
        if isPrensention{
            //如果是开始演示
            //获得将要展现的视图
            let toView = transitionContext.viewForKey(UITransitionContextToViewKey)
            
            //将视图加入容器
            transitionContext.containerView().addSubview(toView!)
            
            //增加动画效果
            toView?.transform = CGAffineTransformMakeScale(1, 0)
            toView?.layer.anchorPoint = CGPointMake(0.5, 0) //设置锚点
            
            UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5, options: nil, animations: { () -> Void in
                
                toView?.transform = CGAffineTransformMakeScale(1, 1)
                
                }, completion: { (_) -> Void in
                    
                    //结束转场
                    transitionContext.completeTransition(true)
                    
            })
            
            
        }else{
            //如果要结束演示
            
            //获得将要消失的视图
            let fromView = transitionContext.viewForKey(UITransitionContextToViewKey)
            
            fromView?.removeFromSuperview()
            
            //结束转场
            transitionContext.completeTransition(true)
            
        }
    }
}
